import warnings
warnings.warn("qturboframe.public_ui.ucf_h_ui.textarea模块已被废弃，替代模块为qturboframe.public_ui.h_ui.textarea", DeprecationWarning)

from ..ucf_base_element import BaseElement
from qturboframe.lib.report.log import logger
from qturboframe.lib.ranorex.exceptions import CsClientError
from qturboframe.lib.ranorex.web_element.div_tag import DivTag
from qturboframe.lib.ranorex.web_element.textarea_tag import TextAreaTag
from qturboframe.lib.utils.check import CheckBasic


class TextArea(BaseElement, CheckBasic):
    '''hui多行文本框操作类'''

    def __init__(self, element_para, father=None, timeout=10000):
        super().__init__(element_para, father, timeout)
        class_list = self._element.get_attribute('class').split(' ')
        if 'h-input-wrapper' not in class_list:
            self._element = self._element.find_element_by_attribute(
                'xpath', './ancestor::div[@class~\'h-input-wrapper\']', timeout=self.timeout)
        self._element = DivTag(self._element.control)
        self.textarea = TextAreaTag(self._element.find_element_by_attribute(
            'xpath', './/*[@class~\'h-input\']', timeout=self.timeout).control)

    def set_value(self, value):
        '''
        hui多行文本框填充

        :Args:
         - text: 填充值

        :Example:
         - 例: textarea.set_value('苹果吃葡萄')
        '''
        self.textarea.focus()
        self.textarea.send_keys(value)
        # from qturboframe.lib.utils.windows.win_desktop import key_input
        # key_input(value)

    def clear(self):
        '''
        hui多行文本框清空

        :Example:
         - 例: textarea.clear()
        '''
        self.textarea.focus()
        self.textarea.clear()
        # from qturboframe.lib.utils.windows.win_desktop import key_press, key_up
        # key_press(['ctrl', 'a'])
        # key_up(['ctrl', 'a'])
        # key_press('backspace')
        # key_up('backspace')

    def is_default(self, value):
        """
        检查hui多行文本框件默认值

        :Args:
         - value: 待检查的值

        :Returns:
            检查匹配返回True，否则返回False

        :Example:
         - 例: textarea.is_default('苹果吃葡萄')
        """
        self.textarea.focus()
        try:
            value_text = self.textarea.get_attribute('value')
        except:
            value_text = None
        try:
            placeholder_text = self.textarea.get_attribute('placeholder')
        except:
            placeholder_text = None
        try:
            title_text = self.textarea.get_attribute('title')
        except:
            title_text = None
        if self.textarea.text:
            text = self.textarea.text
        elif self.textarea.get_attribute('defaultvalue'):
            text = self.textarea.get_attribute('defaultvalue')
        elif value_text:
            text = value_text
        elif placeholder_text:
            text = placeholder_text
        elif title_text:
            text = title_text
        else:
            text = ''
        if text == value:
            return True
        else:
            logger.error('默认值不匹配！')
            return False

    def is_readonly(self):
        '''
        检查hui多行文本框是否只读

        :Return:
            返回文本框是否只读

        :Example:
         - 例: textarea.is_readonly()
        '''
        self.textarea.focus()
        return self.textarea.is_readonly()

    def is_nonnull(self):
        '''
        检查hui多行文本框是否有非空属性

        :Returns:
            非空返回True，否则返回False

        :Example:
         - 例: textarea.is_nonnull()
        '''
        self.textarea.focus()
        form_item = DivTag(self._element.find_element_by_attribute(
            'xpath', './ancestor::div[@class~\'h-form-item-content\']/'
                     'parent::div[@class~\'h-form-item\']', timeout=self.timeout).control)
        item_class_list = form_item.get_attribute('class').split(' ')
        if 'h-form-item-required' in item_class_list:
            return True
        else:
            return False

    def check_default(self, value):
        """
        校验hui多行文本框件默认值

        :Args:
         - value: 待检查的值

        :Raises:
            SeleniumError: 多行文本框的默认值校验不通过

        :Example:
         - 例: textarea.check_default('苹果吃葡萄')
        """
        if not self.is_default(value):
            raise CsClientError('多行文本框的默认值校验不通过！')

    def check_readonly(self):
        '''
        校验hui多行文本框是否只读

        :Raises:
            SeleniumError: 多行文本框的只读校验不通过

        :Example:
         - 例: textarea.check_readonly()
        '''
        if not self.is_readonly():
            raise CsClientError('多行文本框的只读校验不通过！')

    def check_nonnull(self):
        '''
        校验hui文本框是否有非空属性

        :Raises:
            SeleniumError: 多行文本框的非空校验不通过

        :Example:
         - 例: textarea.check_nonnull()
        '''
        if not self.is_nonnull():
            raise CsClientError('多行文本框的非空校验不通过！')

